home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr25 / gg243730.zip / MEMLAB4.C < prev    next >
Text File  |  1993-03-06  |  5KB  |  138 lines

  1. /**********************************************************/
  2. /**********************************************************/
  3. /***                                                    ***/
  4. /***  Program name: MEMLAB4.EXE                         ***/
  5. /***                                                    ***/
  6. /***  Created     : 7 May, 1990                         ***/
  7. /***                                                    ***/
  8. /***  Author      : Bo Falkenberg                       ***/
  9. /***                                                    ***/
  10. /***  Revised     : February, 1992 by Darryl Frost      ***/
  11. /***                                                    ***/
  12. /***  Purpose     : To demonstrate the new system       ***/
  13. /***                limit for the number of threads     ***/
  14. /***                per process in OS/2 2.0 and the     ***/
  15. /***                effect of thread creation on the    ***/
  16. /***                growth of SWAPPER.DAT.              ***/
  17. /***                                                    ***/
  18. /***  Compile     : icc /W2 /Gm+ memlab4.c              ***/
  19. /***                                                    ***/
  20. /***  Execute     : memlab4 n f                         ***/
  21. /***                where n is the number of secondary  ***/
  22. /***                threads the program must create,    ***/
  23. /***                and f if present and 0 (zero) causes***/
  24. /***                the created threads to terminate    ***/
  25. /***                after 40 seconds else the threads   ***/
  26. /***                after 40 seconds enter a wait and   ***/
  27. /***                print loop. The wait time is        ***/
  28. /***                randomly determined. If n is not    ***/
  29. /***                specified 10 is assumed.
  30. /***                                                    ***/
  31. /**********************************************************/
  32. /**********************************************************/
  33.  
  34.  
  35.  
  36. /**********************************************************/
  37. /***  DEFINES                                           ***/
  38. /**********************************************************/
  39. #define INCL_DOS
  40. #define INCL_DOSPROCESS
  41.  
  42.  
  43. /**********************************************************/
  44. /***  INCLUDE                                           ***/
  45. /**********************************************************/
  46. #include <os2.h>
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49.  
  50. void _Optlink NewThread( PVOID pThreadArg ); /* procedure declaration */
  51.  
  52. BOOL loopflag = TRUE;
  53. int threadcount = 0;
  54.  
  55. /**********************************************************/
  56. /***  MAIN PROGRAM                                      ***/
  57. /**********************************************************/
  58. main( int argc, char *argv[], char *envp[] )
  59. {                                         /*************************/
  60.   TID       ThreadID;                     /* thread identification */
  61.   ULONG     ulThreadArg;                  /* thread arguments      */
  62.   ULONG     ulThreadFlags;                /* thread flags          */
  63.   ULONG     ulStack_size;                 /* thread stack size     */
  64.   int       no_of_threads;                /* number of threads     */
  65.   int       i;                            /* loop variable         */
  66.                                           /*************************/
  67.  
  68.   ulThreadFlags = 0;          /* start thread immediately   */
  69.   ulStack_size  = 1024;       /* give stack size in bytes   */
  70.   ulThreadArg   = 1;
  71.  
  72.    if (argc < 2)
  73.       {
  74.          no_of_threads = 10;
  75.       } else
  76.       {
  77.          no_of_threads    = atoi(argv[1]);
  78.       }
  79.   if (argc > 2)
  80.   {
  81.      if (*argv[2]=='0')
  82.         loopflag = FALSE;
  83.   }
  84.  
  85.   for (i = 1; i < no_of_threads+1; i++)
  86.   {
  87.     if ( ( ThreadID = _beginthread( NewThread, NULL, ulStack_size, (PVOID)i ) ) == -1 )
  88.     {
  89.       printf("_beginthread error\n");
  90.       exit (1);
  91.     }
  92.     printf("Thread number %d created\n",i);
  93.   }
  94.  
  95.   printf("To end the program press <CR> \n");
  96.   getchar();
  97.   loopflag = FALSE;
  98.   printf("MEMLAB4 terminating\n");
  99. /* Wait for all the threads to stop */
  100.   while (TRUE)
  101.   {
  102.      DosSleep(1000);
  103.      DosEnterCritSec();
  104.      if(threadcount == 0){
  105.         DosExitCritSec();
  106.         printf("All threads stopped, program is terminated\n");
  107.         exit (1);
  108.      }
  109.      DosExitCritSec();
  110.   }
  111. }
  112.  
  113. /**********************************************************/
  114. /***  THREAD                                            ***/
  115. /**********************************************************/
  116. void NewThread( PVOID pThreadArg )
  117.    {
  118.    ULONG ulThreadArg = ( ULONG )pThreadArg;
  119.  
  120.    DosEnterCritSec();
  121.    threadcount++;
  122.    DosExitCritSec();
  123.  
  124.    printf( "Thread %u has started\n", ulThreadArg );
  125.    srand( (int)ulThreadArg); /* seed random generator */
  126.    DosSleep ( 40000 );       /* 40 SEC. sleep interval */
  127.  
  128.    while (loopflag)
  129.       {
  130.       printf("Thread %d just woke up\n", ulThreadArg);
  131.       DosSleep ( rand() ); /* random sleep interval */
  132.       }
  133.  
  134.    DosEnterCritSec();
  135.    threadcount--;
  136.    DosExitCritSec();
  137.    }
  138.